﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BASE;

public partial class Admin_sinhvien : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //check quyen vao trang

        if (Utils.getIntSession("userid") == 0)
        {
            Response.Redirect("/admin/login.aspx");
        }
        busMenu busM = new busMenu();
        if (!busM.checkQuyenVaoTrang(Utils.getIntSession("userid"), Constants.ADMIN_SINHVIEN))
        {
            Response.Redirect("/admin/");
        }
        Session["trang"] = Constants.ADMIN_SINHVIEN;
        dr_khoa.SelectedIndexChanged += new EventHandler(dr_khoa_SelectedIndexChanged);
        grid.RowDataBound += new GridViewRowEventHandler(grid_RowDataBound);
        if (!IsPostBack)
        {
            bindrop();
            //bindata(dr_khoa.SelectedValue);
        }
    }

    void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList dr_gt2 = new DropDownList();

            dr_gt2 = (DropDownList)e.Row.FindControl("dr_gt");
            if (dr_gt2 != null)
            {
                dr_gt2.Items.Insert(0, new ListItem("--chon--", "3"));
                dr_gt2.Items.FindByValue(DataBinder.Eval(e.Row.DataItem, "GioiTinh").ToString()).Selected = true;
            }

            dr_gt2 = (DropDownList)e.Row.FindControl("dr_dt");
            busDanToc busDT = new busDanToc();
            dr_gt2.DataSource = busDT.getALL();
            dr_gt2.DataTextField = "Ten";
            dr_gt2.DataValueField = "dantocid";
            dr_gt2.DataBind();
            dr_gt2.Items.Insert(0, new ListItem("--chọn--", "0"));
            ListItem item = new ListItem();
            dr_gt2.Items.FindByValue(DataBinder.Eval(e.Row.DataItem, "dantoc").ToString()).Selected = true;

        }
    }

    void dr_khoa_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dr_khoa.SelectedValue != "0")
        {
            UpdatePanel panel = (UpdatePanel)Master.FindControl("UpdatePanel1");
            AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
            trigger.ControlID = ((Control)sender).ID;
            panel.Triggers.Add(trigger);
            panel.DataBind();
            MessenBox(1, "F");
            bindata(dr_khoa.SelectedValue);
        }
    }
    void bindata(string malop)
    {
        busSinhVien busSV = new busSinhVien();
        List<entSinhVien> lst = busSV.getSinhVienByMaLop(malop);
        if (lst.Count == 0)
        {
            

            MessenBox(1,"Lớp chưa có sinh viên");
            btn_luu.Visible = false;
        }
        else
        {
            lb_me.Text = "";
            btn_luu.Visible = true;
        }
        grid.DataSource = lst;
        grid.DataBind();
       
    }

    void bindrop()
    {
        busLop bus = new busLop();
        dr_khoa.DataSource = bus.getByMaKhoa(Utils.getStringSession("khoa"), Utils.getStringSession("userid"));
        dr_khoa.DataTextField = "tenlop";
        dr_khoa.DataValueField = "malop";
        dr_khoa.DataBind();
        dr_khoa.Items.Insert(0, new ListItem("----Chọn lớp----", "0"));
    }
    protected void btn_luu_Click(object sender, EventArgs e)
    {
        List<entSinhVien> lst = new List<entSinhVien>();
        lst = getListToGrid();
        busSinhVien busSV = new busSinhVien();
        entSinhVien entSV = new entSinhVien();

        //check quyen quan ly sinh vien

        ////////////////////////////////
        foreach (entSinhVien item in lst)
        {
            entSV = new entSinhVien();
            busSV.updateAlllylich(item);
        }
    }
    List<entSinhVien> getListToGrid()
    {
        List<entSinhVien> lst = new List<entSinhVien>();
        entSinhVien entSV;

        foreach (GridViewRow row in grid.Rows)
        {
            entSV = new entSinhVien();
            entSV.MaSinhVien = ((Label)row.FindControl("txt_masinhvien")).Text;
            entSV.Ho = ((TextBox)row.FindControl("txt_ho")).Text;
            entSV.Ten = ((TextBox)row.FindControl("txt_ten")).Text;
            entSV.NgaySinh = ((TextBox)row.FindControl("txt_ngaysinh")).Text;
            entSV.GioiTinh = int.Parse(((DropDownList)row.FindControl("dr_gt")).SelectedValue);
            entSV.NoiSinh = ((TextBox)row.FindControl("txt_noisinh")).Text;
            entSV.HoKhau = ((TextBox)row.FindControl("txt_hokhau")).Text;
            entSV.DanToc = int.Parse(((DropDownList)row.FindControl("dr_dt")).SelectedValue);
            entSV.TonGiao = ((TextBox)row.FindControl("txt_tongiao")).Text;
            entSV.HoTenBo = ((TextBox)row.FindControl("txt_hotenbo")).Text;
            entSV.NNBo = ((TextBox)row.FindControl("txt_nnbo")).Text;
            entSV.HoTenMe = ((TextBox)row.FindControl("txt_hotenme")).Text;
            entSV.NNMe = ((TextBox)row.FindControl("txt_nnme")).Text;
            entSV.NamTN12 = ((TextBox)row.FindControl("txt_namtn12")).Text;
            entSV.CMTND = ((TextBox)row.FindControl("txt_cmtnd")).Text;
            entSV.NgayCap = ((TextBox)row.FindControl("txt_ngaycap")).Text;
            entSV.NoiCap = ((TextBox)row.FindControl("txt_noicap")).Text;
            entSV.DiaChi = ((TextBox)row.FindControl("txt_diachi")).Text;
            entSV.DienThoai = ((TextBox)row.FindControl("txt_dienthoai")).Text;
            entSV.Email = ((TextBox)row.FindControl("txt_email")).Text;
            entSV.DiaChiHT = ((TextBox)row.FindControl("txt_diachiht")).Text;
            entSV.GhiChu = ((TextBox)row.FindControl("txt_ghichu")).Text;
            entSV.SoHieuBang12 = ((TextBox)row.FindControl("txt_sohieubang12")).Text;
            lst.Add(entSV);
        }
        return lst;
    }
    protected void link_add_Click(object sender, EventArgs e)
    {
       // AddRowsToGrid();
    }
    private void AddRowsToGrid()
    {
        List<entSinhVien> lst = new List<entSinhVien>();
        entSinhVien entSV = new entSinhVien();
        lst = getListToGrid();
        entSV = new entSinhVien();
        lst.Add(entSV);
        grid.DataSource = lst;
        grid.DataBind();
    }
}